<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<?php use_javascript('jquery.ui.core.js') ?>
<?php use_javascript('jquery.ui.datepicker.js') ?>
<?php use_javascript('date.js') ?>

<form action="<?php echo url_for('compras/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
<?php if (!$form->getObject()->isNew()): ?>
<input type="hidden" name="sf_method" value="put" />
<?php endif; ?>

<?php if ($form->getObject()->isNew()): ?>
<input type="hidden" name="compra_internacional[estado]" value="Borrador" />
<?php endif; ?>

<div id="formulario">
  
  <table width="100%" style="text-align: left;">
    <tbody>
      <?php echo $form->renderGlobalErrors() ?>
      <tr>
        <th><?php echo $form['cuenta_id']->renderLabel() ?></th>
        <th><?php echo $form['nombre_encargado']->renderLabel() ?></th>
        <th><?php echo $form['correo_encargado']->renderLabel() ?></th>
        <th><?php echo $form['anexo_encargado']->renderLabel() ?></th>
      </tr>
      
      <tr>
        <td><?php echo $form['cuenta_id']->renderError() ?> <?php echo $form['cuenta_id'] ?></td>
        <td><?php echo $form['nombre_encargado']->renderError() ?> <?php echo $form['nombre_encargado'] ?></td>
        <td><?php echo $form['correo_encargado']->renderError() ?> <?php echo $form['correo_encargado'] ?></td>
        <td><?php echo $form['anexo_encargado']->renderError() ?> <?php echo $form['anexo_encargado'] ?></td>
      </tr>
    
      <tr>
        <th><?php echo $form['proveedor_id']->renderLabel() ?></th>
        <th><?php echo $form['nombre_contacto']->renderLabel() ?></th>
        <th><?php echo $form['correo_contacto']->renderLabel() ?></th>
        <th><?php echo $form['telefono_contacto']->renderLabel() ?></th>
      </tr>
      
      <tr>
        <td><?php echo $form['proveedor_id']->renderError() ?> <?php echo $form['proveedor_id'] ?></td>
        <td><?php echo $form['nombre_contacto']->renderError() ?> <?php echo $form['nombre_contacto'] ?></td>
        <td><?php echo $form['correo_contacto']->renderError() ?> <?php echo $form['correo_contacto'] ?></td>
        <td><?php echo $form['telefono_contacto']->renderError() ?> <?php echo $form['telefono_contacto'] ?></td>
      </tr>

      <tr>
        <th><?php echo $form['moneda_id']->renderLabel() ?></th>
        <th>Cotizaci&oacute;n</th>
        <th>Otros</th>
        <th><?php echo $form['agencia_de_aduanas']->renderLabel() ?> </th>
      </tr>

      <tr>
        <td><?php echo $form['moneda_id']->renderError() ?> <?php echo $form['moneda_id'] ?></td>
        <td><?php echo $form['cotizacion']->renderError() ?> <?php echo $form['cotizacion'] ?></td>
        <td><?php echo $form['otros']->renderError() ?> <?php echo $form['otros'] ?></td>
        <td><?php echo $form['agencia_de_aduanas']->renderError() ?> <?php echo $form['agencia_de_aduanas'] ?></td>
      </tr>
      
      <tr>
      	<th><?php echo $form['forma_pago']->renderLabel() ?></th>
      	<th>
      		<?php if ($sf_user->hasCredential('Internacional')): ?>
      			<?php echo $form['plazo_entrega']->renderLabel() ?>
      		<?php endif; ?>
      	</th>
      	<th></th>
      	<th><?php echo $form['freight_forwarder']->renderLabel() ?></th>
      </tr>
      
      <tr>
      	<td><?php echo $form['forma_pago']->renderError() ?> <?php echo $form['forma_pago'] ?></td>
      	<td>
      		<?php if ($sf_user->hasCredential('Internacional')): ?>
      			<?php echo $form['plazo_entrega']->renderError() ?> <?php echo $form['plazo_entrega'] ?>
      		<?php endif; ?>
      	</td>
      	<td></td>
      	<td><?php echo $form['freight_forwarder']->renderError() ?> <?php echo $form['freight_forwarder'] ?></td>
      </tr>
      
      <tr>
      	<th colspan="4"><?php echo $form['observacion']->renderLabel() ?></th>
      </tr>
      
      <tr>
      	<td colspan="4" id="obs"><?php echo $form['observacion']->renderError() ?> <?php echo $form['observacion'] ?></td>
      </tr>
      
    </tbody>
  </table>

<br>

<table id="contenedor">
  <thead>
    <tr>
      <th>Art&iacute;culo</th>
      <th>Cantidad</th>
      <th>Costo Unitario</th>
      <th>Subtotal</th>
      <th>Acciones</th>
    </tr>
  </thead>
  <tbody>
    <?php if ($form->getObject()->isNew()): ?>
    <script type="text/javascript">newfieldscount = 1;</script>
    <tr>
      <td><?php echo $form['new'][0]['articulo_id']->renderError() ?><?php echo $form['new'][0]['articulo_id'] ?></td>
      <td><?php echo $form['new'][0]['cantidad']->renderError() ?><?php echo $form['new'][0]['cantidad'] ?></td>
      <td><?php echo $form['new'][0]['costo_neto_unitario']->renderError() ?><?php echo $form['new'][0]['costo_neto_unitario'] ?></td>
      <td><?php echo $form['new'][0]['subtotal']->renderError() ?><?php echo $form['new'][0]['subtotal'] ?></td>
      <td><a class="removenew" href="#">eliminar</a></td>
    </tr>
    <?php endif ?>
  
  <?php foreach ($form['ItemCompraInternacional'] as $item):?>
    <tr>
      <td><?php echo $item['articulo_id']->renderError() ?><?php echo $item['articulo_id'] ?></td>
      <td><?php echo $item['cantidad']->renderError() ?><?php echo $item['cantidad'] ?></td>
      <td><?php echo $item['costo_neto_unitario']->renderError() ?><?php echo $item['costo_neto_unitario'] ?></td>
      <td><?php echo $item['subtotal']->renderError() ?><?php echo $item['subtotal'] ?></td>
      <td>Acciones</td>
    </tr>
    <?php endforeach ?>
  </tbody>
  
  <tfoot>
    <tr>
      <td><a id="addoccurrence" href="#">Agregar item</a></td>
    </tr>
  </tfoot>
</table>

<br>

<table width="100%">
  <tbody>
    <tr >
      <td width="20%"></td>
      <td width="20%"></td>
      <td width="20%"></td>
      <td width="20%"> <?php echo $form['total']->renderLabel() ?> </td>
      <td width="20%"> <?php echo $form['total']->renderError() ?> <?php echo $form['total'] ?> </td>
    </tr>
  </tbody>
</table>
  
   <table width="100%">
    <tfoot>
      <tr>
        <td>
          	<?php echo $form->renderHiddenFields(false) ?>
			<?php echo button_to('Volver', 'compras/index') ?>
          	<?php if (!$form->getObject()->isNew()): ?>
          		<?php echo button_to('Eliminar', 'compras/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => 'Estás seguro de eliminar?')) ?>
          	<?php endif; ?>
			<input type="submit" value="Guardar" />
        </td>
      </tr>
    </tfoot>
  </table>  
</div>

<script>
$(document).ready(
  function (){
    $("input[id^=cantidad_]").bind("keyup", recalc);
    $("input[id^=costo_]").bind("keyup", recalc);
    recalc();
  }
);
	
function recalc(){
  $("[id^=subtotal_]").calc(
    "qty * price",
    {
      qty: $("input[id^=cantidad_]"),
      price: $("input[id^=costo_]")
    },

	function (s){
		return s.toFixed(0);
	},

	function ($this){
		var sum = $this.sum();
			
		total = sum.toFixed(0);
	}
  );
  
  $("input#total").val(total);
}
</script>
